Execution
Execution is the process an agent follows to transform a user request into a final response. Rather than sending a prompt directly to the language model, BindAI executes a structured pipeline that prepares context, retrieves knowledge, executes tools when necessary, and produces the final result.Execution Pipeline
A typical execution follows this sequence.Initialization
The execution begins by creating an execution state. This stage:- initializes the execution context
- stores the user input
- prepares the conversation
- builds the initial model request
Knowledge Retrieval
If the agent has an attached knowledge source, BindAI retrieves relevant information before contacting the language model. The retrieved context is appended to the request so the model can answer using external information. If no knowledge source exists, this step is skipped.Memory
If the agent uses memory, previous conversation history is injected into the request. Memory allows the model to maintain context across multiple interactions. Without memory, every execution is independent.Model Generation
After the request is prepared, the provider generates a response. For example:- generated text
- tool calls (if any)
- token usage
- structured output (when requested)
Tool Execution
If the language model requests a tool, BindAI executes it automatically. Example flow:Tool Iterations
Agents can call multiple tools during a single execution.Finish
When the model no longer requests tools, execution completes. The final response is converted into anAgentResult.
Example:
AgentResult
Every execution returns anAgentResult.
Structured Output
If an output model is provided, BindAI attempts to parse the response into that object.UserProfile instead of raw text.
Execution Context
Every execution receives anExecutionContext.
The context stores runtime information such as:
- variables
- execution state
- events
- metadata
Middleware
Middleware can execute code before and after agent execution. Example:- authentication
- logging
- metrics
- request validation
- response transformation
Events
During execution, BindAI publishes lifecycle events. Typical events include:Hooks
Hooks provide another extension point. Unlike events, hooks are attached directly to an agent and execute as part of its lifecycle. They are useful for application-specific behavior after an execution completes.Streaming
Agents can stream tokens instead of waiting for the full response.Error Handling
If an exception occurs during execution, BindAI returns:Execution Flow Example
A complete execution may look like this.Best Practices
- Keep prompts focused.
- Use tools only when necessary.
- Limit tool iterations.
- Use structured output for machine-readable responses.
- Enable streaming for long responses.
- Attach middleware for cross-cutting concerns.
- Use events for monitoring instead of modifying the execution pipeline.
